﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using PeachLite.Entity;

namespace PeachLite.Helpers
{
    /// <summary>
    /// FileHelper静态类
    /// </summary>
    public static class FileHelper
    {
        /// <summary>
        /// 文件夹标识
        /// </summary>
        public const string FOLDER = "folder";

        /// <summary>
        /// 获得指定路径下的文件/文件夹列表
        /// </summary>
        /// <param name="path">物理路径</param>
        /// <returns>指定路径下的文件/文件夹列表</returns>
        public static DataTable GetFile(string path)
        {
            DataTable files = new DataTable();
            DataColumn col0 = new DataColumn("Name", Type.GetType("System.String"));            //文件名
            DataColumn col1 = new DataColumn("Type", Type.GetType("System.String"));            //类型
            files.Columns.Add(col0);
            files.Columns.Add(col1);
            try
            {
                DirectoryInfo di = new DirectoryInfo(path);
                if (!di.Exists)
                {
                    throw new Exception("文件夹路径不存在！");
                }
                //查找是否含有子文件夹
                DirectoryInfo[] dis = di.GetDirectories();
                if (dis.Length > 0)
                {
                    for (int i = 0; i < dis.Length; i++)
                    {
                        DataRow row = files.NewRow();
                        row[0] = Path.GetFileName(dis[i].FullName);
                        row[1] = FOLDER;
                        files.Rows.Add(row);
                    }
                }

                FileInfo[] fis = di.GetFiles();
                if (fis.Length > 0)
                {
                    for (int i = 0; i < fis.Length; i++)
                    {
                        DataRow row = files.NewRow();
                        string extension = Path.GetExtension(fis[i].FullName).ToLower();
                        row[0] = Path.GetFileName(fis[i].FullName);
                        row[1] = extension.Substring(1, extension.Length - 1);
                        files.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return files;
        }

        /// <summary>
        /// 获得文件/文件夹类型
        /// </summary>
        /// <param name="path">文件/文件夹物理路径</param>
        /// <returns>返回类型信息</returns>
        public static string GetFileType(string path)
        {
            if (Directory.Exists(path))
            {
                return FOLDER;
            }
            if (File.Exists(path))
            {
                return Path.GetExtension(path).Replace(".", "");
            }
            else
            {
                return "";
            }
        }

        public static string GetFatherDirectoryPath(string path)
        {
            try
            {
                path = path.Trim().Replace("\\", "/").TrimEnd(new char[] { '/' });

                return path.LastIndexOf('/') < 0 ? path : path.Substring(0, path.LastIndexOf('/'));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除文件/文件夹
        /// </summary>
        /// <param name="path">文件/文件夹物理路径</param>
        /// <returns>返回处理信息</returns>
        public static SysMessage DeleteFile(string path)
        {
            SysMessage m = new SysMessage();
            if (string.IsNullOrWhiteSpace(path))
            {
                m.Success = false;
                m.Message = "文件路径为空！";
                return m;
            }

            //如果是文件夹
            if (Directory.Exists(path))
            {
                try
                {
                    Directory.Delete(path, true);
                    m.Code = 0;
                    m.Success = true;
                }
                catch (Exception ex)
                {
                    m.Success = false;
                    m.Message = ex.ToString();
                }
                return m;
            }

            //如果文件不存在，返回错误信息
            if (!File.Exists(path))
            {
                m.Success = false;
                m.Message = "文件不存在!";
                return m;
            }

            //删除文件
            try
            {
                File.Delete(path);
                m.Code = 0;
                m.Success = true;
            }
            catch (Exception ex)
            {
                m.Success = false;
                m.Message = ex.ToString();
            }
            return m;
        }

        /// <summary>
        /// 获得文件/文件夹图标
        /// </summary>
        /// <param name="source">文件/文件夹完整物理路径</param>
        /// <param name="target">文件/文件夹url</param>
        /// <returns>返回图标文件路径</returns>
        public static string GetIcon(string source, string target)
        {
            //如果是文件夹
            if (Directory.Exists(source))
            {
                //如果包含子文件夹
                if (Directory.GetDirectories(source).Length > 0)
                {
                    return ConfigHelper.Config.FolderHasFolder;
                }
                //如果包含文件
                if (Directory.GetFiles(source).Length > 0)
                {
                    return ConfigHelper.Config.FolderHasFile;
                }
                else
                {
                    return ConfigHelper.Config.FolderEmpty;
                }
            }
            else
            {
                //如果是正常文件
                if (File.Exists(source))
                {
                    string ex = Path.GetExtension(source).ToLower().Replace(".", "");
                    //如果是图片
                    if (ConfigHelper.Config.ImageTypes.Contains(ex))
                    {
                        return target;
                    }
                    else
                    {
                        return ConfigHelper.Config.Icons.Contains(ex) ? ConfigHelper.Config.IconPath + ex + (ConfigHelper.Config.IconExtension.StartsWith(".") ? ConfigHelper.Config.IconExtension : "." + ConfigHelper.Config.IconExtension) : ConfigHelper.Config.DefaultFileIcon;
                    }
                }
                else
                {
                    return "";
                }
            }
        }
    }
}